# 导包
import unittest
from ddt import ddt, data
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from page_model import login
from tools.read_excel import ReadTestData
from tools.tools import Tools

drug = ReadTestData('./test_data/mms_test_delete.xls')
drug_yp_t = drug.read_excel('yp')  # 读取删除顾客功能的正向测试数据
drug_gk_t = drug.read_excel('gk')  # 读取删除药品功能的正向测试数据
drug_jbr_t = drug.read_excel('jbr')  # 读取删除经办人功能的正向测试数据

# 删除的测试用例类
@ddt
class MmsDeleteTest(unittest.TestCase):

    def setUp(self) -> None:
        # 创建浏览器对象
        self.dr = webdriver.Chrome()
        self.dr.implicitly_wait(5)
        self.dr.maximize_window()
        self.lg = login.Login(self.dr)  # 调用登录页面类

    def tearDown(self) -> None:
        time.sleep(3)
        self.dr.quit()

    # 删除顾客的测试用例
    @data(*drug_gk_t)
    def testGkDelete(self, drug):
        self.lg.login(drug)
        # 点击删除按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[3]/div[1]/div[1]').click()
        # 点击删除顾客
        self.dr.find_element(By.XPATH, '//*[@id="DC"]/a[1]/span/span[1]').click()
        # 确定循环次数
        num = int(self.dr.find_element(By.XPATH,
                                       '//*[@id="output"]/div[2]/div[7]/div/div/div[2]/div[3]/table/tbody/tr/td[8]/span').text)
        # 在页面中循环查找确定的编号
        for i in range(0, num):
            # 将页面中所有编号放进一个列表
            ls = self.dr.find_elements(By.XPATH, '//*[field="cno"]')
            c = 0
            # 循环这个列表
            for l in ls:
                # 如果有相等的编号
                if l.text == drug['num']:
                    # 点击他的兄弟元素
                    l.find_element(By.XPATH, './parent::*').find_element(By.XPATH,
                                                                         './descendant::*[@field="GenreId"]').click()
                    time.sleep(0.5)
                    # 点击删除信息
                    self.dr.find_element(By.XPATH, '//*[@id="clientDelete"]/a/span/span[1]').click()
                    time.sleep(0.5)
                    # 断言- 查看弹窗中是否弹出删除成功
                    try:
                        msg = self.dr.find_element(By.XPATH,
                                                   '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
                        ex = '删除成功'
                        self.assertEqual(ex, msg, '出错啦')
                    except Exception as e:
                        Tools.save_screenshot(self.dr, 'drug_add')
                        raise e
                    # 点击确定
                    self.dr.find_element(By.XPATH, '/html/body/div[10]/div[2]/div[3]/a/span').click()
                    c += 1
                    break
            if c == 1:
                break
            # 点击下一页
            self.dr.find_element(By.XPATH,
                                 '/html/body/div[1]/div[3]/div/div[2]/div/div/div/div[2]/div[3]/table/tbody/tr/td[10]/a/span/span[2]').click()
            time.sleep(2)
        else:
            print('没找到')
            raise Exception

    # 删除经办人的测试用例
    @data(*drug_jbr_t)
    def testJbrDelete(self, drug):
        self.lg.login(drug)
        # 点击删除按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[3]/div[1]/div[1]').click()
        # 点击删除经办人
        self.dr.find_element(By.XPATH, '//*[@id="DC"]/a[2]/span/span[1]').click()
        # 确定循环次数
        num = int(self.dr.find_element(By.XPATH,
                                       '//*[@id="output"]/div[2]/div[6]/div/div/div[2]/div[3]/table/tbody/tr/td[8]/span').text)
        # 在页面中循环查找确定的编号
        for i in range(0, num):
            # 将页面中所有编号放进一个列表
            ls = self.dr.find_elements(By.XPATH, '//*[field="ano"]')
            c = 0
            # 循环这个列表
            for l in ls:
                # 如果有相等的编号
                if l.text == drug['num']:
                    # 点击他的兄弟元素
                    l.find_element(By.XPATH, './parent::*').find_element(By.XPATH,
                                                                         './descendant::*[@field="GenreId"]').click()
                    time.sleep(0.5)
                    # 点击删除信息
                    self.dr.find_element(By.XPATH, '//*[@id="agencyDelete"]/a/span/span[1]').click()
                    time.sleep(0.5)
                    # 断言- 查看弹窗中是否弹出删除成功
                    try:
                        msg = self.dr.find_element(By.XPATH,
                                                   '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
                        ex = '删除成功'
                        self.assertEqual(ex, msg, '出错啦')
                    except Exception as e:
                        Tools.save_screenshot(self.dr, 'drug_add')
                        raise e
                    # 点击确定
                    self.dr.find_element(By.XPATH, '/html/body/div[10]/div[2]/div[3]/a/span').click()
                    c += 1
                    break
            if c == 1:
                break
            # 点击下一页
            self.dr.find_element(By.XPATH,
                                 '/html/body/div[1]/div[3]/div/div[2]/div/div/div/div[2]/div[3]/table/tbody/tr/td[10]/a/span/span[2]').click()
            time.sleep(2)
        else:
            print('没找到')
            raise Exception

    # 删除药品的测试用例
    @data(*drug_yp_t)
    def testYpDelete(self, drug):
        self.lg.login(drug)
        # 点击删除按钮
        self.dr.find_element(By.XPATH, '//*[@id="accordion"]/div[3]/div[1]/div[1]').click()
        # 点击删除药品
        self.dr.find_element(By.XPATH, '//*[@id="DC"]/a[3]/span/span[1]').click()
        # 确定循环次数
        num = int(self.dr.find_element(By.XPATH,
                                       '//*[@id="output"]/div[2]/div[7]/div/div/div[2]/div[3]/table/tbody/tr/td[8]/span').text)
        # 在页面中循环查找确定的编号
        for i in range(0, num):
            # 将页面中所有编号放进一个列表
            ls = self.dr.find_elements(By.XPATH, '//*[field="mno"]')
            c = 0
            # 循环这个列表
            for l in ls:
                # 如果有相等的编号
                if l.text == drug['num']:
                    # 点击他的兄弟元素
                    l.find_element(By.XPATH, './parent::*').find_element(By.XPATH,
                                                                         './descendant::*[@field="GenreId"]').click()
                    time.sleep(0.5)
                    # 点击删除信息
                    self.dr.find_element(By.XPATH, '//*[@id="medicineDelete"]/a/span/span[1]').click()
                    time.sleep(0.5)
                    # 断言- 查看弹窗中是否弹出删除成功
                    try:
                        msg = self.dr.find_element(By.XPATH,
                                                   '//*[@class="messager-body panel-body panel-body-noborder window-body"]/div[0]').text
                        ex = '删除成功'
                        self.assertEqual(ex, msg, '出错啦')
                    except Exception as e:
                        Tools.save_screenshot(self.dr, 'drug_add')
                        raise e
                    # 点击确定
                    self.dr.find_element(By.XPATH, '/html/body/div[10]/div[2]/div[3]/a/span').click()
                    c += 1
                    break
            if c == 1:
                break
            # 点击下一页
            self.dr.find_element(By.XPATH,
                                 '/html/body/div[1]/div[3]/div/div[2]/div/div/div/div[2]/div[3]/table/tbody/tr/td[10]/a/span/span[2]').click()
            time.sleep(2)
        else:
            print('没找到')
            raise Exception


if __name__ == '__main__':
    unittest.main()
